$(function () {

    // 重新开始游戏按钮事件
    $(".icon-zhongxinshiyang").click(function () {
        $('#cover-content').html(`<!-- 询问游戏重新开始 -->
        <div class="select_bg"></div>
        <div class="select_opt">
            <div class="select_determine"></div>
            <div class="select_cancel"></div>
        </div>`)
        showModal()

        $('.select_determine').click(function(){
            newGame()
            closeModal()
            if(score>window.localStorage.getItem('2048bestScore')){
                window.localStorage.setItem('2048bestScore',score)
            }
        })

        $('.select_cancel').click(function(){
            closeModal()
        })
    });

    // 难度选择按钮事件
    $('.icon-xiala').click(function(){
        $('#cover-content').html(`<div class="difficulty_bg">
            <div class="difficulty_1"></div>
            <div class="difficulty_2"></div>
            <div class="difficulty_3"></div>
            <div class="difficulty_close"></div>
        </div>`)
        showModal()
        $('.difficulty_1').click(function(){
            rows = 4
            cols = 4
            gridWidth = domWidth*0.92   // 棋盘格的宽度
            cellLength = domWidth*0.18   // 每个格子的边长
            cellSpace = domWidth*0.04   // 间隙的长度
            prepareMobile()
            newGame()
            closeModal()
        })
        $('.difficulty_2').click(function(){
            rows = 5
            cols = 5
            gridWidth = domWidth*0.92   // 棋盘格的宽度
            cellLength = domWidth*0.15   // 每个格子的边长
            cellSpace = domWidth*0.028   // 间隙的长度
            prepareMobile()
            newGame()
            closeModal()
        })
        $('.difficulty_3').click(function(){
            rows = 6
            cols = 6
            gridWidth = domWidth*0.92   // 棋盘格的宽度
            cellLength = domWidth*0.13   // 每个格子的边长
            cellSpace = domWidth*0.02   // 间隙的长度
            prepareMobile()
            newGame()
            closeModal()
        })

        $('.difficulty_close').click(function(){
            closeModal()
        })
        $('#cover-content').click(()=>{
            // closeModal()
        })
    })


    // 遊戲幫助按鈕事件
    $('.icon-bangzhu').click(function(){
        $('#cover-content').html(`<div class="help_bg"></div>
        <div class="help_close"><span>关闭</span></div>`)
        showModal()

        $('.help_close').click(function(){
            closeModal()
        })
    })
    
    $('#cover-bg').click(function(){
        closeModal()
    })

    // 二维码
    $('.icon-duanxinlianxiwo').click(function(){
        $('#cover-content').html(`<div class="lianxi">
        <img src="./img/ewm.png" width="100%" alt="">
    </div>`)
        showModal()
    })

    // 分享
    $('.icon-Group-').click(function(){
        $('#cover-content').html(`<div class="lianxi">
        <img src="./img/fenxiang.png" width="100%" alt="">
    </div>`)
        showModal()
    })
});